package jdk.jdk8.localdate;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * java8
 */
public class LocalDateTest {
    public static void main(String[] args) {
        //date2LocalDate();
        //format();

        //plus();
        //compare();
        //between();

        // 打印8月开始的5个自然周
        int month = 10;
        LocalDateTime dateTime = LocalDateTime.of(2022, 8, 1, 21, 2, 1);
        LocalDateTime monday = dateTime;
        int num = 5;
        while (num >= 0) {
            monday = monday.with(DayOfWeek.MONDAY);
            LocalDateTime sunday = monday.with(DayOfWeek.SUNDAY);
            System.out.println(LocalDateTimeUtils.format(monday,"yyyy-MM-dd")+"===>"+LocalDateTimeUtils.format(sunday,"yyyy-MM-dd"));
            monday = monday.plusWeeks(1);
            num--;
        }

    }

    private static void between() {
        LocalDateTime dateTime1 = LocalDateTime.of(2022, 8, 1, 12, 23, 21);
        LocalDateTime dateTime2 = LocalDateTime.of(2022, 9, 20, 1, 23, 21);
        long between = LocalDateTimeUtils.between(dateTime1, dateTime2, ChronoUnit.MONTHS, true);
        System.out.println(between);
    }

    private static void compare() {
        LocalDateTime dateTime = LocalDateTime.of(2022, 1, 1, 12, 23, 21);
        LocalDateTime now = LocalDateTime.of(2022, 1, 1, 12, 23, 21);
        System.out.println(now.isAfter(dateTime));
        System.out.println(now.isBefore(dateTime));
        System.out.println(now.isEqual(dateTime));

        LocalDate localDate = LocalDate.of(2022, 1, 1);
        LocalDate localDate1 = dateTime.toLocalDate();
        System.out.println(localDate.isAfter(localDate1));
        System.out.println(localDate.isBefore(localDate1));
        System.out.println(localDate.isEqual(localDate1));
    }

    private static void plus() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(LocalDateTimeUtils.format(dateTime,LocalDateTimeUtils.DEFAULT_DATE_PATTERN));
        System.out.println("加一天:"+LocalDateTimeUtils.formatAtDefault(dateTime.plusDays(1)));
        System.out.println("减一天:"+LocalDateTimeUtils.formatAtDefault(dateTime.plusDays(-1)));
        System.out.println("加一周:"+LocalDateTimeUtils.formatAtDefault(dateTime.plusWeeks(1)));
        System.out.println("减一周:"+LocalDateTimeUtils.formatAtDefault(dateTime.plusWeeks(-1)));
        System.out.println("加一年:"+LocalDateTimeUtils.formatAtDefault(dateTime.plusYears(1)));
        System.out.println("减一年:"+LocalDateTimeUtils.formatAtDefault(dateTime.plusYears(-1)));
    }

    private static void format() {
        LocalTime now = LocalTime.now();
        String format = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println(format);
    }

    private static void date2LocalDate() {
        LocalDateTime localDateTime = LocalDateTimeUtils.date2LocalDateTime(new Date());
        String format = LocalDateTimeUtils.format(localDateTime, LocalDateTimeUtils.DEFAULT_DATE_PATTERN);
        System.out.println(format);


        LocalDateTime dateTime = LocalDateTimeUtils.parse("2022-01-01 12:21:12", LocalDateTimeUtils.DEFAULT_DATE_PATTERN);
        String format1 = LocalDateTimeUtils.format(dateTime, LocalDateTimeUtils.DEFAULT_DATE_PATTERN);
        System.out.println(format1);

        LocalDate localDate = dateTime.toLocalDate();
        String format2 = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(format2);
    }
}
